using UnityEngine;
using System.Collections;

public class BSPDungeonGenerationlevel : MonoBehaviour {
	// This Algorithm comes from the Basic BSP Dungeon Generation from RougeBasin
    //http://roguebasin.roguelikedevelopment.org/index.php/Basic_BSP_Dungeon_generation
	
	public float MD; // min distance from edge of screen
	public float miW; // min width
	public float maW; // max width
	public float miH; // min height
	public float maH; // max height
	public float WRmi; //width range min
	public float WRma; //width range max
	public float HRmi; // height range min
	public float HRma; // height range max
	public float Mapwidth; // width min and max together
	public float Mapheight; // height min and max together
	public Rect cell; // current cell of dungeon 
	public Rect parent; // root cell of dungeon
	public Texture2D texture; //texture
	public Texture2D texture4; //texture
	public Texture2D texture2; //texture
	public bool drawtexture;
	public bool horizontal = false;
	public bool vertical = false;
	public int ButtonPress = 1;
	public string stringToEdit = " ";
	public bool generateroom;
	public int random = 1;
	public bool hallway;
	public int switcher = 1;
	
	// Use this for initialization
	void Start () 
	{
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		/*miW = 70;
		maW = 90;
		miH = 60;
		maH = 65;
		
		
		WRma = cell.miW * maW;
		HRmi = cell.maH * miH;
		
		Mapwidth = miW * maW;
		Mapheight = miH * maH;
		if ( WRma > 90)
		{
			WRma = cell.Mapwidth - 2 * MD;
		}
		
		else
		{
			WRmi = cell.Mapwidth * miW;
			HRma = cell.Mapheight * maH;
		}
		
		if (HRma > 65)
		{
			HRma = cell.Mapheight - 2 * MD;
		}
		
		else
		{
			GenerateRoom();
		}*/
		
	}
		
	void OnGUI()
	{
		GUI.Label(new Rect(170,10,200,20), "Number of Splits:");
		stringToEdit = GUI.TextField(new Rect(300, 10, 200, 20), stringToEdit, 25);
		
		if(stringToEdit =="1")
		{
			
			TextSplit1();
		}
		
		if(stringToEdit =="2")
		{
			TextSplit2();
		}
		
		if(stringToEdit =="3")
		{
			TextSplit3();
		}
		
		if(stringToEdit =="4")
		{
			DrawTextureHorizontal();
			DrawTextureVertical();
			DrawTextureVertical3();
			DrawTextureVertical2();
			DrawTextureHorizontal2();
			DrawTextureVertical4();
			generateroom = true;
			random++;
			hallway = true;
		}
		
		if(GUI.Button(new Rect(10,90,200,20), "Generate Rooms"))
		{
			if (ButtonPress == 1)
			{
				
			}
			else
			{
			  generateroom = true;
			  random++;
			}
		}
		
		if(GUI.Button(new Rect(10,170,200,20), "Generate Corridors"))
		{
			if (ButtonPress == 1)
			{
				
			}
			else
			  hallway = true;
		}
		if(generateroom == true)
		{
			GenerateRoom();
		}
	
		if (GUI.Button(new Rect(10,50,200,20), "Split"))
		{
			
			//drawtexture = true;
			ButtonPress++;
		}
		
		if(GUI.Button(new Rect(10,130,200,20), "Generate All"))
		{
			if (ButtonPress == 1)
			{
				
			}
			else
			{
			hallway = true;
			generateroom = true;
			random++;
			}
		}
		
		if(GUI.Button(new Rect(10,210,200,20), "Reset"))
		{
			
			switcher++;
		}
		
		if (hallway == true)
		{
			GenerateHallway();
		}
	
		if (drawtexture == true)
		{
			DrawTextureVertical();
		}
		
		if(ButtonPress >=2)
		{
			DrawTextureHorizontal();
		}
		
		if(ButtonPress >= 3)
		{
			DrawTextureVertical2();
		}
		
		if(ButtonPress >= 4)
		{
			DrawTextureHorizontal2();
		}
		
		if(ButtonPress >= 5)
		{
			DrawTextureVertical3();
		}
		
		if(ButtonPress >= 5)
		{
			DrawTextureVertical4();
		}
		
		if(ButtonPress >=6)
		{
			DrawTextureVertical5();
		}
		
		if(switcher >=2)
		{
			Switcher1();
		}
		
		if(switcher >=3)
		{
			Switcher2();
		}
	}
    
	void GenerateRoom()
	{
		/*cell = new Rect(miW,maW,miH,maH);
		room = Graphics.DrawTexture(new Rect(miW,maW,miH,maH), texture);//generate room
		parent =  new Rect(220,50,400,400);
		room.width = MapWidth;
		room.height = MapHeight;
		
		Minroom.x = MD;
		Minroom.y = MD;
		Maxroom.x = cell.Mapwidth - room.Mapwidth - MD;
		Maxroom.y = cell.Mapheight - room.Mapheight - MD;
		
		room.y = room.y + cell.y;
		room.x = room.x + cell.x;
		
		cell.room = room;
		
		parent = room + cell.parent; */
		
		Graphics.DrawTexture(new Rect(253, 140, 40, 50), texture);
		Graphics.DrawTexture(new Rect(225, 54, 70, 50),  texture);
		Graphics.DrawTexture(new Rect(370, 54, 70, 30),  texture);
		Graphics.DrawTexture(new Rect(480, 57, 30, 40),  texture);
		Graphics.DrawTexture(new Rect(380, 105, 70, 90),  texture);
		Graphics.DrawTexture(new Rect(480, 110, 70, 70),  texture);
		Graphics.DrawTexture(new Rect(500, 207, 50, 50),  texture);
		Graphics.DrawTexture(new Rect(400, 219, 30, 30),  texture);
		Graphics.DrawTexture(new Rect(375, 305, 90, 90),  texture);
		Graphics.DrawTexture(new Rect(490, 315, 50, 50),  texture);
		Graphics.DrawTexture(new Rect(230, 227, 40, 80),  texture);
		Graphics.DrawTexture(new Rect(310, 247, 40, 80),  texture);
		Graphics.DrawTexture(new Rect(230, 353, 80, 40),  texture);
	} 
	
	
	void DrawTexture()
	{
		if(horizontal == false)
		{
			vertical = true;
			horizontal = true;
			DrawTextureVertical();
		}
		
	   else if ( horizontal == true)
		{
			vertical = false;
			horizontal = false;
			DrawTextureHorizontal();
		}

	}
	
	void DrawTextureHorizontal()
	{
		Graphics.DrawTexture(new Rect(220,200,350,1),texture4); // split
		Graphics.DrawTexture(new Rect(220,200,350,1),texture4); // split
		
	}
	
	void DrawTextureVertical()
	{
		Graphics.DrawTexture(new Rect(360,50,1,350), texture4); //split
	}
	
	void DrawTextureHorizontal2()
	{
		Graphics.DrawTexture(new Rect(360,50,1,350), texture4); //split
		Graphics.DrawTexture(new Rect(360,100,209,1),texture4); // split
		Graphics.DrawTexture(new Rect(360,300,209,1),texture4); // split
	}
	
	void DrawTextureVertical2()
	{
		Graphics.DrawTexture(new Rect(360,50,1,350), texture4); //split
		
	}
	
	void DrawTextureVertical3()
	{
		Graphics.DrawTexture(new Rect(360,50,1,350), texture4); //split
		Graphics.DrawTexture(new Rect(360,50,1,350), texture4); //split
		Graphics.DrawTexture(new Rect(360,100,209,1),texture4); // split
		Graphics.DrawTexture(new Rect(360,300,209,1),texture4); // split
		Graphics.DrawTexture(new Rect(220,200,350,1),texture4); // split
		Graphics.DrawTexture(new Rect(470,50,1,350),texture4); // split
	}
	
	void DrawTextureVertical4()
	{
		Graphics.DrawTexture(new Rect(360,50,1,350), texture4); //split
		Graphics.DrawTexture(new Rect(360,50,1,350), texture4); //split
		Graphics.DrawTexture(new Rect(360,100,209,1),texture4); // split
		Graphics.DrawTexture(new Rect(360,300,209,1),texture4); // split
		Graphics.DrawTexture(new Rect(220,200,350,1),texture4); // split
		Graphics.DrawTexture(new Rect(470,50,1,350),texture4); // split
		Graphics.DrawTexture(new Rect(220,350,140,1),texture4); // split
		Graphics.DrawTexture(new Rect(290,200,1,150),texture4); // split
	}
	
	void DrawTextureVertical5()
	{
		Graphics.DrawTexture(new Rect(360,50,1,350), texture4); //split
		Graphics.DrawTexture(new Rect(360,50,1,350), texture4); //split
		Graphics.DrawTexture(new Rect(360,100,209,1),texture4); // split
		Graphics.DrawTexture(new Rect(360,300,209,1),texture4); // split
		Graphics.DrawTexture(new Rect(220,200,350,1),texture4); // split
		Graphics.DrawTexture(new Rect(470,50,1,350),texture4); // split
		Graphics.DrawTexture(new Rect(220,350,140,1),texture4); // split
		Graphics.DrawTexture(new Rect(290,200,1,150),texture4); // split
		Graphics.DrawTexture(new Rect(220,120,140,1),texture4); // split
	}
	
	void GenerateHallway()
	{
		Graphics.DrawTexture(new Rect(240,306,2,47),texture2); // hallway
		Graphics.DrawTexture(new Rect(350,246,50,2),texture2); // hallway
		Graphics.DrawTexture(new Rect(350,246,50,2),texture2); // hallway
		Graphics.DrawTexture(new Rect(430,236,70,2),texture2); // hallway
		Graphics.DrawTexture(new Rect(309,360,67,2),texture2); // hallway
		Graphics.DrawTexture(new Rect(464,360,26,2),texture2); // hallway
		Graphics.DrawTexture(new Rect(520,257,2,59),texture2); // hallway
		Graphics.DrawTexture(new Rect(520,180,2,28),texture2); // hallway
		Graphics.DrawTexture(new Rect(450,140,30,2),texture2); // hallway
		Graphics.DrawTexture(new Rect(440,70,40,2),texture2); // hallway
		Graphics.DrawTexture(new Rect(295,70,75,2),texture2); // hallway
		Graphics.DrawTexture(new Rect(290,104,2,36),texture2); // hallway
		Graphics.DrawTexture(new Rect(490,95,2,15),texture2); // hallway
	}
	
	void TextSplit1()
	{
		Graphics.DrawTexture(new Rect(250, 98, 100, 100), texture);
		Graphics.DrawTexture(new Rect(300, 300, 100, 50), texture);
		Graphics.DrawTexture(new Rect(340,198,2,102),texture2); // hallway
		Graphics.DrawTexture(new Rect(220,200,350,1),texture4); // split
	}
	
	void TextSplit2()
	{
		Graphics.DrawTexture(new Rect(250, 98, 70, 70), texture);
		Graphics.DrawTexture(new Rect(300, 300, 100, 50), texture);
		Graphics.DrawTexture(new Rect(447, 145, 50, 50), texture);
		Graphics.DrawTexture(new Rect(447, 250, 100, 30), texture);
		Graphics.DrawTexture(new Rect(315,168,2,132),texture2); // hallway
		Graphics.DrawTexture(new Rect(315,168,132,2),texture2); // hallway
		Graphics.DrawTexture(new Rect(450,194,2,56),texture2); // hallway
		Graphics.DrawTexture(new Rect(220,200,350,1),texture4); // split
		Graphics.DrawTexture(new Rect(400,50,1,350), texture4); //split
	}
	
	void TextSplit3()
	{
		Graphics.DrawTexture(new Rect(220,200,350,1),texture4); // split
		Graphics.DrawTexture(new Rect(400,50,1,350), texture4); //split
		Graphics.DrawTexture(new Rect(220,120,180,1),texture4); // split
		Graphics.DrawTexture(new Rect(400,290,170,1),texture4); // split
		
		Graphics.DrawTexture(new Rect(260, 130, 50, 50), texture);
		Graphics.DrawTexture(new Rect(305, 53, 70, 50),  texture);
		Graphics.DrawTexture(new Rect(405, 54, 120, 120),texture);
		Graphics.DrawTexture(new Rect(410, 205, 90, 30), texture);
		Graphics.DrawTexture(new Rect(455, 305, 70, 90), texture);
		Graphics.DrawTexture(new Rect(305, 317, 70, 70), texture);
		
		Graphics.DrawTexture(new Rect(470,235,2,70),texture2); // hallway
		Graphics.DrawTexture(new Rect(470,174,2,32),texture2); // hallway
		Graphics.DrawTexture(new Rect(374,90,31,2),texture2); // hallway
		Graphics.DrawTexture(new Rect(374,325,81,2),texture2); // hallway
		Graphics.DrawTexture(new Rect(308,103,2,27),texture2); // hallway
	}
	
	void TextSplit4()
	{
		Graphics.DrawTexture(new Rect(360,50,1,350), texture4); //split
		Graphics.DrawTexture(new Rect(360,50,1,350), texture4); //split
		Graphics.DrawTexture(new Rect(360,100,209,1),texture4); // split
		Graphics.DrawTexture(new Rect(360,300,209,1),texture4); // split
		Graphics.DrawTexture(new Rect(220,200,350,1),texture4); // split
		Graphics.DrawTexture(new Rect(470,50,1,350),texture4); // split
		Graphics.DrawTexture(new Rect(220,350,140,1),texture4); // split
		Graphics.DrawTexture(new Rect(290,200,1,150),texture4); // split
		Graphics.DrawTexture(new Rect(220,120,140,1),texture4); // split 
		
		
	}
	
	void Switcher1()
	{
		Application.LoadLevel("BSP 1");
	}
	
	void Switcher2()
	{
		Application.LoadLevel("BSP");
	}

}